#!/bin/bash

# ELAdmin Docker 镜像构建脚本

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

# 默认配置
BACKEND_IMAGE_NAME="eladmin"
FRONTEND_IMAGE_NAME="eladmin-web"
IMAGE_TAG="latest"
FRONTEND_DOCKERFILE="../eladmin-web/Dockerfile"
FRONTEND_BUILD_CONTEXT="../eladmin-web"

# 打印函数
print_message() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

print_step() {
    echo -e "${BLUE}[STEP]${NC} $1"
}

# 检查Docker环境
check_docker() {
    if ! command -v docker &> /dev/null; then
        print_error "Docker 未安装，请先安装 Docker"
        exit 1
    fi
    
    if ! docker info &> /dev/null; then
        print_error "Docker 服务未运行，请启动 Docker 服务"
        exit 1
    fi
    
    print_message "Docker 环境检查通过"
}

# 构建JAR包
build_jar() {

    print_warning "JAR 包开始构建..."
    mvn clean package -DSkipTests
    
}

# 检查前端项目
check_frontend() {
    if [ ! -d "$FRONTEND_BUILD_CONTEXT" ]; then
        print_error "前端目录不存在: $FRONTEND_BUILD_CONTEXT"
        print_error "请确保 eladmin-web 目录在正确位置"
        exit 1
    fi
    
    if [ ! -f "$FRONTEND_DOCKERFILE" ]; then
        print_error "前端 Dockerfile 不存在: $FRONTEND_DOCKERFILE"
        print_error "请确保前端项目包含 Dockerfile"
        exit 1
    fi
    
    print_message "前端项目检查通过: $FRONTEND_BUILD_CONTEXT"
}

# 构建后端镜像
build_backend_image() {
    print_step "开始构建后端 Docker 镜像..."
    
    BUILD_CMD="docker build -t ${BACKEND_IMAGE_NAME}:${IMAGE_TAG}"
    
    if [ "$IMAGE_TAG" != "latest" ]; then
        BUILD_CMD="$BUILD_CMD -t ${BACKEND_IMAGE_NAME}:latest"
    fi
    
    BUILD_CMD="$BUILD_CMD -f Dockerfile ."
    
    if [ "$NO_CACHE" = "true" ]; then
        BUILD_CMD="$BUILD_CMD --no-cache"
    fi
    
    print_message "执行后端构建命令: $BUILD_CMD"
    
    if eval $BUILD_CMD; then
        print_message "后端镜像构建成功！"
        print_message "后端镜像名称: ${BACKEND_IMAGE_NAME}:${IMAGE_TAG}"
        docker images | grep "^${BACKEND_IMAGE_NAME}"
    else
        print_error "后端镜像构建失败"
        exit 1
    fi
}

# 构建前端镜像
build_frontend_image() {
    print_step "开始构建前端 Docker 镜像..."
    
    BUILD_CMD="docker build -t ${FRONTEND_IMAGE_NAME}:${IMAGE_TAG}"
    
    if [ "$IMAGE_TAG" != "latest" ]; then
        BUILD_CMD="$BUILD_CMD -t ${FRONTEND_IMAGE_NAME}:latest"
    fi
    
    BUILD_CMD="$BUILD_CMD -f ${FRONTEND_DOCKERFILE} ${FRONTEND_BUILD_CONTEXT}"
    
    if [ "$NO_CACHE" = "true" ]; then
        BUILD_CMD="$BUILD_CMD --no-cache"
    fi
    
    print_message "执行前端构建命令: $BUILD_CMD"
    
    if eval $BUILD_CMD; then
        print_message "前端镜像构建成功！"
        print_message "前端镜像名称: ${FRONTEND_IMAGE_NAME}:${IMAGE_TAG}"
        docker images | grep "^${FRONTEND_IMAGE_NAME}"
    else
        print_error "前端镜像构建失败"
        exit 1
    fi
}

# 显示构建信息
show_build_info() {
    print_message "构建完成！"
    echo ""
    echo "镜像信息:"
    echo "  后端镜像: ${BACKEND_IMAGE_NAME}:${IMAGE_TAG}"
    echo "    大小: $(docker images ${BACKEND_IMAGE_NAME}:${IMAGE_TAG} --format 'table {{.Size}}' | tail -n 1)"
    echo "  前端镜像: ${FRONTEND_IMAGE_NAME}:${IMAGE_TAG}"
    echo "    大小: $(docker images ${FRONTEND_IMAGE_NAME}:${IMAGE_TAG} --format 'table {{.Size}}' | tail -n 1)"
    echo ""
    echo "使用命令:"
    echo "  后端: docker run -d -p 8000:8000 ${BACKEND_IMAGE_NAME}:${IMAGE_TAG}"
    echo "  前端: docker run -d -p 80:80 ${FRONTEND_IMAGE_NAME}:${IMAGE_TAG}"
    echo "  完整部署: docker-compose up -d"
    echo ""
}

# 主函数
main() {
    print_message "ELAdmin Docker 镜像构建脚本"
    echo "=================================="
    
    print_message "构建配置:"
    echo "  后端镜像名称: $BACKEND_IMAGE_NAME"
    echo "  前端镜像名称: $FRONTEND_IMAGE_NAME"
    echo "  镜像标签: $IMAGE_TAG"
    echo "  不使用缓存: ${NO_CACHE:-false}"
    echo ""
    
    check_docker
    build_jar
    
    build_backend_image

    
    build_frontend_image
    
    show_build_info
}

main "$@"
